﻿using Common.Contract.Authentication.Record;
using Common.Contract.HuntingCustomer.Record;
using Common.Contract.HuntingCustomer.Request;
using Common.Contract.HuntingCustomer.Response;
using Common.Contract.Paging;
using Common.Contract.SystemConfig.Record;
using Common.Service.Command;
using SalePerformance.Model.Interface;
using SalePerformance.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SalePerformance.Core.ServiceCommand.HuntingCustomer
{
    public class RetrieveCustomerHuntingByDaoServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveCustomerHuntingByDaoRequest, RetrieveCustomerHuntingByDaoResponse>
    {
        #region Properties
        public UserRecord Dao;
        public FilterRecord TextSearch;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
        #endregion
        public RetrieveCustomerHuntingByDaoServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveCustomerHuntingByDaoRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.CurrentUser;
            }
                TextSearch = Request.Filter;
            if (Request.Page != null)
            {
                pageIndex = Request.Page.PageIndex ?? 0;
                pageSize = Request.Page.PageSize ?? 0;
            }
        }

        protected override bool OnExecute(RetrieveCustomerHuntingByDaoResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            try
            {
                List<CustomerHuntingRecord> records = new List<CustomerHuntingRecord>();
                List<TblHuntingCustomer> listCustomer = new List<TblHuntingCustomer>();
                listCustomer = Providers.HuntingCustomer.RetrieveListCustomerHunting(out totalRecord, Dao, TextSearch, Request.Lang, pageSize, pageIndex);
                if (listCustomer != null)
                {
                    records = listCustomer.Select(x => new CustomerHuntingRecord
                    {
                        CompanyAddress = x.CompanyAddress,
                        Createtime = x.Createtime,
                        UserDaoAssign = x.UserDaoAssign,
                        UserDaoName = x.UserDaoName,
                        BranchCode = x.BranhCode,
                        BranchCodeSme = x.BranchCodeSme,
                        BranchName = x.BranchName,
                        BranchNameMap = x.BranchNameMap,
                        BranchNameSme = x.BranchNameSme,
                        CompanyEmail = x.CompanyEmail,
                        CompanyInfo = x.CompanyInfo,
                        CompanyName = x.CompanyName,
                        CompanyPhone = x.CompanyPhone,
                        ContactStatus = x.ContactStatus,
                        Dao = x.Dao,
                        DaoName = x.DaoName,
                        Leadid = x.Leadid,
                        LeadSource = x.LeadSource,
                        LeadStatus = x.LeadStatus,
                        LegalId = x.LegalId,
                        Modifiedtime = x.Modifiedtime,
                        ProductCategory = x.ProductCategory,
                        ProductName = x.ProductName,
                        ProductNo = x.ProductNo,
                        Region = x.Region,
                        RegionMap = x.RegionMap,
                        RelationEmail = x.RelationEmail,
                        RelationName = x.RelationName,
                        RelationPhone = x.RelationPhone
                    }).ToList();
                }
                response.Records = records;
                response.Page = new PageRecord
                {
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    TotalRecords = totalRecord
                };
            }
            catch (Exception e)
            {
                isSuccess = false;
                errorMessage = e.Message;
            }

            response.ErrorMessage = errorMessage;
            return isSuccess;
        }
    }
}
